$(function () {
    $('.remove-session').on('click',function () {
        clientCommon.removeSession('hot-condition')
    });

    _getBaseDate.getCarListNumber(renderPage,{marketId:1,carSaleState:20});
    function renderPage(list) {
        $('#car-numbers').text('全国有' + list + '辆车')
    }
    //获取热门品牌
    function getHotBrandList(list) {
        var hotBrandList = [];
        for(var i=0;i<list.length;i++){
            if(list[i].brandId=='108'||list[i].brandId=='25'||list[i].brandId=='1'||list[i].brandId=='36'||list[i].brandId=='7'||list[i].brandId=='5'||list[i].brandId=='9'||list[i].brandId=='35'||list[i].brandId=='6'||list[i].brandId=='125'){
                hotBrandList.push(list[i])
            }else{
                continue
            }
        }
        return hotBrandList
    }
    //渲染品牌列表
    function renderBrandList(list) {
        list = getHotBrandList(list);
        for(var i=0;i<list.length;i++){
            $('#brandList').append('<a data-id="'+ 'brandId=' + list[i].brandId +'" href="' + '../../html/list/list.html?brandId=' + list[i].brandId +'" class="car-brand"><img src="'+ list[i].brandLogo + '">' + '<span>' + list[i].brandName + '</span></a>');
        }
        $('#brandList a').on('click',function () {
            clientCommon.setSession('hot-condition',$(this).attr('data-id'));
        })
    }
    _getBaseDate.getCarBrandList(renderBrandList);
    //渲染车身类型列表
    function renderCarTypeList(list) {
        list = list.splice(0,12);
        for(var i=0;i<list.length;i++){
            $('#type-car-box').append('<a data-id="'+ 'carTypeId=' + list[i].carTypeId +'" href="' + '../../html/list/list.html?carTypeId=' + list[i].carTypeId +'" class="car-type"><img src="'+ '../../images/' + list[i].carTypeId + '.png' + '">' + '<span>' + list[i].carTypeName + '</span></a>');
        }
        $('#type-car-box a').on('click',function () {
            clientCommon.setSession('hot-condition',$(this).attr('data-id'));
        })
    }
    _getBaseDate.getCarTypeList(renderCarTypeList);

    $('#car-year-box a').on('click',function () {
        clientCommon.setSession('hot-condition',$(this).attr('data-id'));
    })

    //渲染里程列表
    function renderCarMileageList(list) {
        for(var i=0;i<list.length;i++){
            $('.car-mile-box').append('<a data-id="'+ 'carMileageId=' + list[i].carMileageId +'" href="' + '../../html/list/list.html?fromMileage=' + list[i].carMileageFrom + '&toMileage=' + list[i].carMileageTo +'">' + list[i].carMileageRemark + '</a>');
        }
        $('#car-mile-box a').on('click',function () {
            clientCommon.setSession('hot-condition',$(this).attr('data-id'));
        })
    }
    _getBaseDate.getCarMileageList(renderCarMileageList);

    //渲染车辆价格列表
    function renderCarPriceList(list) {
        for(var i=0;i<list.length;i++){
            $('.car-price-box').append('<a data-id="'+ 'priceId=' + list[i].priceId +'" href="' + '../../html/list/list.html?fromRetailPrice=' + list[i].priceFrom10000 + '&toRetailPrice=' + list[i].priceTo10000 +'">' + list[i].priceRemark + '</a>');
        }
        $('#car-price-box a').on('click',function () {
            clientCommon.setSession('hot-condition',$(this).attr('data-id'));
        })
    }
    _getBaseDate.getPriceList(renderCarPriceList);

    //渲染车辆列表
    function renderCarList(list){
        var validateImg,p1,p;
        for(var i=0;i<list.length;i++){
            list[i].carLicenceDate = clientCommon.checkValue(list[i].carLicenceDate);
            list[i].carMileage = clientCommon.checkValue(list[i].carMileage);
            list[i].carTitle = clientCommon.checkValue(list[i].carTitle);
            list[i].carRetailPrice = clientCommon.checkValue(list[i].carRetailPrice);
            list[i].carLicenceDate = clientCommon.handleDate('yyyy-MM',list[i].carLicenceDate);
            if(list[i].carAuthState == 10){
                validateImg = '<img class="QY-validate-img" src="../../images/认证icon04@2x.png">';
            }else{
                validateImg = ''
            }
            console.log(list[i].carRetailPrice)
            if(list[i].carRetailPrice == '--'||list[i].carRetailPrice == 0){
                p1 = '<p class="car-item-price item-common">' + "面议" + validateImg + '</p>';
            }else{
                p1 = '<p class="car-item-price item-common"><span>￥</span>' + list[i].carRetailPrice + "万元" + validateImg + '</p>'
            }
            console.log(p1)
            if(list[i].carLicenceState == 1){
                p = '<p class="car-sub-info item-common">' + list[i].carLicenceDate + "上牌 " + list[i].carMileage + "万公里" + '</p>'
            }else if(list[i].carLicenceState == 3){
                p = '<p class="car-sub-info item-common">' + "新车 " + list[i].carMileage + "万公里" + '</p>'
            }else{
                p = '<p class="car-sub-info item-common">' + "未上牌 " + list[i].carMileage + "万公里" + '</p>'
            }
            $('#car-List').append('<a data-id="' + list[i].carId + '" class="car-item"><div class="car-item-box">\n' +
                '<img src="' + imageUrl + list[i].carImageUrl + '.r_290xh_230' + '">\n' +
                '<p class="car-info item-common ">' + list[i].carTitle + '</p>\n' + p + p1 +
                '</div></a>');
        }
        $('.list-content-box').hide();
        $('.list-content-box').height($('.list-content-box').height());
        $('.list-content-box').show();
    }
    _getBaseDate.getCarList(renderCarList,{marketId:1,carSaleState:20,pageSize:8,pageIndex:1,orderColunm:'CAR_SALE_TIME',orderMode:'DESC'});

    //打开完善信息窗口
    $('#saleCar').on('click',function () {
        if(!clientCommon.getLocal('_loginInfo')){
            layer.msg('请先登录您的账号')
        }else{
            $('#tip').show();
            $('#saleInfoBox').show();
            _getBaseDate.getCarBrandList(renderSelectBrandList);
            _getBaseDate.getProvinceList(renderProvinceList);
        }
    });

    //渲染车系下拉列表
    function renderSeriesList(list) {
        $('#series').append('<option value="">请选择</option>');
        for(var i=0;i<list.length;i++){
            $('#series').append('<option value="' + list[i].seriesId + '">' +  list[i].seriesName + '</option>');
        }
    }
    $('#brand').on('change',function () {
        if($('#brand').val() == ''){
            $("#series").empty();
        }else{
            $("#series").empty();
            _getBaseDate.getSeriesList(renderSeriesList,{brandId:$('#brand').val()})
        }
    });
    //渲染品牌下拉列表
    function renderSelectBrandList(list) {
        for(var i=0;i<list.length;i++){
            $('#brand').append('<option value="' + list[i].brandId + '">' +  list[i].brandName + '</option>');
        }
    }
    //渲染省份列表
    function renderProvinceList(list) {
        $('#province').append('<option value="">请选择</option>');
        for(var i=0;i<list.length;i++){
            $('#province').append('<option value="' + list[i].provinceId + '">' +  list[i].provinceName + '</option>');
        }
    }
    //渲染城市下拉列表
    function renderCityList(list) {
        $('#city').append('<option value="">请选择</option>');
        for(var i=0;i<list.length;i++){
            $('#city').append('<option value="' + list[i].cityId + '">' +  list[i].cityName + '</option>');
        }
    }
    $('#province').on('change',function () {
        if($('#province').val() == ''){
            $("#city").empty();
        }else{
            $("#city").empty();
            _getBaseDate.getCityList(renderCityList,{provinceId:$('#province').val()})
        }
    });
    //调起日历插件
    $('#cal-box').calendar({
        ifSwitch: true, // 是否切换月份
        hoverDate: true, // hover是否显示当天信息
        backToday: true // 是否返回当天
    });
    $('#cal-box').hide();
    $('.calendar').on('click',function () {
        $('#cal-box').show();
        $('.calendar-date .item').on('click',function (e) {
            var year = $(this).attr('data').substring(0,4);
            var month = $(this).attr('data').substring(4,6);
            var day = $(this).attr('data').substring(6,8);
            $('.calendar-date .item').attr('class','item item-curMonth');
            $(this).attr('class','item item-curDay');
            $('#calendar').val(year + '-' + month + '-' + day);
            $('#cal-box').hide()
        });
    });
    //提交信息
    $('.submit-info').on('click',function () {
        var param = {
            brandId:$('#brand').val(),
            brandName:$("#brand").find("option:selected").text(),
            cityId:$('#city').val(),
            cityName:$("#city").find("option:selected").text(),
            sellCarLicenceDate:$('#calendar').val(),
            sellCarMileage:$('#mile').val(),
            seriesId:$('#series').val(),
            seriesName:$("#series").find("option:selected").text(),
            userId:clientCommon.getLocal('_loginInfo').userId
        };
        if($('#brand').val()&&$('#series').val()&&$('#calendar').val()&&$('#mile').val()&&$('#province').val()&&$('#city').val()){
            function dealResponse(data,xhr) {
                if(data.code == 1){
                    clientCommon.setLocal('Authorization',xhr.getResponseHeader('Authorization'));
                    layer.msg('卖车预约提交成功，我们将尽快与您取得联系');
                    $('#tip').hide();
                    $('saleInfoBox').hide();
                }else{
                    console.log(data.msg);
                    layer.msg('信息验证失败,请重新尝试');
                }
            }
            clientCommon.ajax($apiAddress.insertSellCar,param,dealResponse);
        }else{
            layer.msg('信息不全,提交失败');
            return false;
        }
    });

    $('.optimization-car').on('click',function () {//优选车
        window.location.href = '../../html/list/list.html?carAuthState=10';
    });
    $('.choiceness-car').on('click',function () {//精选SUV
        window.location.href = '../../html/list/list.html?chioceSuv=true';
    });
    $('.luxury-car').on('click',function () {//豪华跑车
        window.location.href = '../../html/list/list.html?carTypeId=13';
    });
    //切换启辕优选
    $('#best-choice').on('click',function () {
        $("#car-List").empty();
        $("#best-choice").attr("class","active-bg");
        $("#best-choice").removeAttr("type-border");
        $("#new-publish").attr("class","type-border");
        $("#new-publish").removeAttr("active-bg");
        $('.list-content-box').height('auto');
        _getBaseDate.getCarList(renderCarList,{marketId:1,carSaleState:20,pageSize:8,pageIndex:1,carAuthState:10});
    });
    //切换最新发布
    $('#new-publish').on('click',function () {
        $("#car-List").empty();
        $("#new-publish").attr("class","active-bg");
        $("#new-publish").removeAttr("type-border");
        $("#best-choice").attr("class","type-border");
        $("#best-choice").removeAttr("active-bg");
        _getBaseDate.getCarList(renderCarList,{marketId:1,carSaleState:20,pageSize:8,pageIndex:1,orderColunm:'CAR_SALE_TIME',orderMode:'DESC'});
    })


});